<?php
use WeChat\Pay;
/**
 * Created by PhpStorm.
 * User: Guangpeng Chen
 * Date: 15-5-30
 * Time: 上午9:20
 */
if (!is_numeric($_POST['money']) || !$_POST['money']) {
    echo '{"status":"fail","msg":"金额不对"}';
    exit;
}
if (!$_SESSION['memberID']){
    exit('{"status":"fail","msg":"会员ID为空"}');
}

if (!$_SESSION['openid']){
    exit('{"status":"fail","msg":"OPENID为空"}');
}
$money = floatval($_POST['money']);
$aid   = intval($_POST['aid']);
$did   = intval($_POST['did']);
//$did = $_SESSION['memberID'];
$name_list = array();
$total_fee = $money * 100;

include '/var/www/html/wx/wechat/lanewechat.php';
$sql = "SELECT dname,id FROM pft_member WHERE id IN($aid, $did) LIMIT 2";
$stmt = \LaneWeChat\Core\Db::Connect()->prepare($sql);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $name_list[$row['id']] = $row['dname'];
}
$body   = "[{$name_list[$did]}]给{$name_list[$aid]}充值{$money}元|{$did}|$aid";

$appid  = $_POST['appid'];
$WePayConf = include '/var/www/html/wx/pay/wepay/WxPayPubHelper/WePay.conf.php';

$mchid  = $WePayConf[$appid]['mchid'];
$key    = $WePayConf[$appid]['key'];
$app_secret = $WePayConf[$appid]['app_secret'];

define('SSLCERT_PATH',$WePayConf[$appid]['sslcert_path']);
define('SSLKEY_PATH', $WePayConf[$appid]['sslkey_path']);
///var/www/html/wx/wepay/WxPayPubHelper/WePay.conf.php
include("/var/www/html/wx/pay/wepay/WxPayPubHelper/WePay.class.php");
$pub_obj = new WeChat\Pay2\Common_util_pub($appid, $mchid, $key, $app_secret);

$out_trade_no = "w{$did}_{$_SERVER['REQUEST_TIME']}".$pub_obj->createNoncestr(10);
$sourceT = 1;
if ($_POST['pay_type']=='alipay') {
    $sourceT = 0;
}
$str=<<<SQL
INSERT pft_alipay_rec SET out_trade_no='$out_trade_no',subject='$body',
total_fee='$money',description='$body',sourceT=$sourceT
SQL;
$ret = \LaneWeChat\Core\Db::Connect()->exec($str);
if ($ret===false) {
    echo '{"status":"fail","msg":"记录时错误"}';
    exit;
}
//支付宝充值
if ($_POST['pay_type']=='alipay') {
    echo '{"status":"ok","msg":"","data":{"out_trade_no":"'.$out_trade_no.'","subject":"'.$body.'"}}';
    exit;
}
//微信充值：使用jsapi接口
$jsApi = new WeChat\Pay2\JsApi_pub($appid, $mchid, $key, $app_secret);
$openid = $_SESSION['openid'];
//$openid = 'oNbmEuDdAEWDS_a02HYFlzNYFUTg';
//=========步骤2：使用统一支付接口，获取prepay_id============
//使用统一支付接口
$unifiedOrder = new WeChat\Pay2\UnifiedOrder_pub($appid, $mchid, $key, $app_secret);
//设置统一支付接口参数
$unifiedOrder->setParameter("openid", $openid);//
$unifiedOrder->setParameter("body", $body);//商品描述
$unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号
$unifiedOrder->setParameter("total_fee", $total_fee);//总金额，单位分
$unifiedOrder->setParameter("notify_url",'http://wx.12301.cc/pay/wepay/notify_url_recharge.php');//通知地址
$unifiedOrder->setParameter("trade_type","JSAPI");//交易类型
$prepay_id = $unifiedOrder->getPrepayId();
//var_dump($prepay_id);
//=========步骤3：使用jsapi调起支付============
$jsApi->setPrepayId($prepay_id);
$jsApiParameters = $jsApi->getParameters();
echo '{"status":"ok","msg":"","data":'.$jsApiParameters.'}';



